#!/bin/bash
branch_v=$1
user_id=$2
build_id=$3
status=$4
roll_num=$5
date_stamp=`date "+%Y-%m-%d %H:%M:%S"`
date1=`date "+%Y-%m-%d-%H-%M-%S"`
echo "$brance_v" 
DIANSHU_WEBSITE_HOME=$HOME/dianshu-website-frontend/
#创建备份目录
echo $date_stamp
if [ ! -d $DIANSHU_WEBSITE_HOME/bak/$build_id ];then
  mkdir -p $DIANSHU_WEBSITE_HOME/bak/$build_id
  cp -r $DIANSHU_WEBSITE_HOME/dist/*  $DIANSHU_WEBSITE_HOME/bak/$build_id/
fi
########### 判断部署还是回滚 ###############################
if [ "$status" = "Deploy" ]; then
	echo "开始正常部署"
	rm -rf /www/dianshu-website/*
	cp -r $DIANSHU_WEBSITE_HOME/dist/*   /www/dianshu-website
	sudo nginx -s reload
	send_weixin_v2   "【部署推送】\n【$date_stamp】\n【典枢】\n【frontend-website/前端/】\n【典枢Demo环境】【fenglin测试环境/43.138.115.87】\n【$branch_v】 分支部署完成, 由 $2 用户构建部署 "
elif [ -d $DIANSHU_WEBSITE_HOME/bak/$roll_num  ] && [ -f $DIANSHU_WEBSITE_HOME/bak/$roll_num/index.html  ]; then
	echo "开始回滚操作"
	rm -rf /www/dianshu-website/*
	cp -r $DIANSHU_WEBSITE_HOME/bak/$roll_num/*   /www/dianshu-website
	sudo nginx -s reload
	send_weixin_v2   "【部署推送】\n【$date_stamp】\n【典枢】\n【frontend-website/前端/】\n【典枢Demo环境】【fenglin测试环境/43.138.115.87】\n【$branch_v】 分支部署完成,回滚版本为$roll_num 由 $2 用户构建部署 "
fi